package com.glls.order.service.impl;

import com.glls.common.vo.OrderVo;
import com.glls.order.feign.AccountApi;
import com.glls.order.feign.StockApi;
import com.glls.order.mapper.OrderMapper;
import com.glls.order.service.AccountService;
import com.glls.order.service.OrderService;
import com.glls.order.service.StockService;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrdeiServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private StockService stockService;

    @Resource
    private AccountService accountService;

    @Override
    public int addOrderWithTransaction(OrderVo order) {
        //添加订单
        int result = orderMapper.addOrder(order);

        //扣减库存
        int result2 = stockService.updateStock(order);

        //扣减余额
        int result3 = accountService.updateAccount(order);

        return result3;
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public int addOrderWithLocalTransaction(OrderVo order) throws ClassNotFoundException {
        //添加订单
        int result = orderMapper.addOrder(order);

        //扣减库存
        int result2 = stockService.updateStock(order);



        //扣减余额
        int result3 = accountService.updateAccount(order);


        //Class.forName("java.lang.String2");
        return result3;
    }

    @Resource
    private AccountApi accountApi;

    @Resource
    private StockApi stockApi;


    //演示分步式事务
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)



    @GlobalTransactional //全局事务开启
    public int addOrderWithDistributeTransaction(OrderVo order) {
        //添加订单
        int result = orderMapper.addOrder(order);

        //扣减库存
        int result2 = stockApi.updateStock(order);



        //扣减余额
        int result3 = accountApi.updateAccount(order);


        //Class.forName("java.lang.String2");

        return result3;
    }


}
